package egar

import (
	"fmt"
	"log"
)

type InPacketType = int8

const (
	InPacketTypeStartGame InPacketType = iota + 1
	InPacketTypeMoveMouse
	InPacketTypeVomitOn
	InPacketTypeVomitOff
	InPacketTypeSplit
	InPacketTypeChatMessage
)

type packetHandler struct {
	player *Player
}

func newPacketHandler(player *Player) *packetHandler {
	return &packetHandler{
		player: player,
	}
}

func (p *packetHandler) onPacket(packet []byte) {
	buffer := NewBufferFormData(packet)
	switch packetType := buffer.ReadInt8(); packetType {
	case InPacketTypeStartGame:
		p.player.Name = string(buffer.ReadBytes(buffer.ReadableBytes(), false))
		p.player.protocol.startGameOk()
	case InPacketTypeMoveMouse:
		p.player.TargetX = float64(buffer.ReadFloat32(false))
		p.player.TargetY = float64(buffer.ReadFloat32(false))
	case InPacketTypeVomitOn:
		p.player.Vomiting = true
	case InPacketTypeVomitOff:
		p.player.Vomiting = false
	case InPacketTypeSplit:
		p.player.Spliting = true
	case InPacketTypeChatMessage:
		msg := string(buffer.ReadBytes(buffer.ReadableBytes(), false))
		fmt.Printf("chat msg = %s\n", msg)
	default:
		log.Printf("unkown in-packet type: 0x%x\n", packetType)
	}
}
